home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del ratón / CheckerWithKeyboard / CheckerWithKeyboard.cs next >
Encoding:
Text File  |  2002-04-18  |  1.8 KB  |  60 lines

  1. //--------------------------------------------------
  2. // CheckerWithKeyboard.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CheckerWithKeyboard: Checker
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new CheckerWithKeyboard());
  13.      }
  14.      public CheckerWithKeyboard()
  15.      {
  16.           Text += " con Interfaz de teclado";
  17.      }
  18.      protected override void OnGotFocus(EventArgs ea)
  19.      {
  20.           Cursor.Show();
  21.      }
  22.      protected override void OnLostFocus(EventArgs ea)
  23.      {
  24.           Cursor.Hide();
  25.      }
  26.      protected override void OnKeyDown(KeyEventArgs kea)
  27.      {
  28.           Point ptCursor = PointToClient(Cursor.Position);
  29.           
  30.           int x = Math.Max(0, Math.Min(xNum - 1, ptCursor.X / cxBlock));
  31.           int y = Math.Max(0, Math.Min(yNum - 1, ptCursor.Y / cyBlock));
  32.  
  33.           switch(kea.KeyCode)
  34.           {
  35.           case Keys.Up:    y--;  break;
  36.           case Keys.Down:  y++;  break;
  37.           case Keys.Left:  x--;  break;
  38.           case Keys.Right: x++;  break;
  39.  
  40.           case Keys.Home:  x = y = 0;     break;
  41.           case Keys.End:   x = xNum - 1;
  42.                            y = yNum - 1;  break;
  43.           case Keys.Enter:
  44.           case Keys.Space:
  45.                abChecked[y, x] ^= true;
  46.                Invalidate(new Rectangle(x * cxBlock, y * cyBlock,
  47.                                         cxBlock, cyBlock));
  48.                return;
  49.  
  50.           default:
  51.                return;
  52.           }
  53.           x = (x + xNum) % xNum;
  54.           y = (y + yNum) % yNum;
  55.  
  56.           Cursor.Position = PointToScreen(new Point(x*cxBlock + cxBlock/2,
  57.                                                     y*cyBlock + cyBlock/2));
  58.      }
  59. }
  60.